package edu.zbtc.ie.sunxd.prac0615;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import org.w3c.dom.Text;

import java.util.List;

public class ListFragment extends Fragment {
    private RecyclerView mRecyclerViewMark;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_list, container, false);
        mRecyclerViewMark = v.findViewById(R.id.recycler_view_mark);
        mRecyclerViewMark.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRecyclerViewMark.setAdapter(new ListAdapter(MarkLab.get().getMarks()));
        return v;
    }

    private class ListAdapter extends RecyclerView.Adapter<ListViewHolder> {
        private List<Mark> mMarks;

        public ListAdapter(List<Mark> marks) {
            mMarks = marks;
        }

        @NonNull
        @Override
        public ListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            LayoutInflater inflater = LayoutInflater.from(getActivity());
            return new ListViewHolder(inflater, parent);
        }

        @Override
        public void onBindViewHolder(@NonNull ListViewHolder holder, int position) {
            Mark m = mMarks.get(position);
            holder.bind(m);
        }

        @Override
        public int getItemCount() {
            return mMarks.size();
        }
    }

    private class ListViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private Mark mMark;
        private TextView mTVStudentID;
        private TextView mTVChinese;
        private TextView mTVMath;
        private TextView mTVEnglish;
        private TextView mTVNature;

        public ListViewHolder(LayoutInflater inflater, ViewGroup parent) {
            super(inflater.inflate(R.layout.list_item_mark, parent, false));

            mTVStudentID = itemView.findViewById(R.id.tv_student_id);
            mTVChinese = itemView.findViewById(R.id.tv_chinese);
            mTVMath = itemView.findViewById(R.id.tv_math);
            mTVEnglish = itemView.findViewById(R.id.tv_english);
            mTVNature = itemView.findViewById(R.id.tv_nature);

            itemView.setOnClickListener(this);
        }

        public void bind(Mark mark) {
            mMark = mark;
            mTVStudentID.setText("学号：" + mark.getStudentID());
            mTVChinese.setText("语文：" + mark.getChinese());
            mTVMath.setText("数学：" + mark.getMath());
            mTVEnglish.setText("英语：" + mark.getEnglish());
            mTVNature.setText("理综：" + mark.getNature());
        }

        @Override
        public void onClick(View v) {
            int total = mMark.getChinese() + mMark.getMath() + mMark.getEnglish() + mMark.getNature();
            Toast.makeText(getActivity(), "总分：" + total, Toast.LENGTH_SHORT).show();
        }
    }
}
